package com.fhyc.controller;

import com.fhyc.common.Result;
import com.fhyc.service.FileService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文件同步读取
 */
//ModelAnalysisMX
@Slf4j
@RestController
@RequestMapping("/api/fm")
@RequiredArgsConstructor
public class FileReadController {

    @Resource
    private FileService fileService;

    @GetMapping("/readFile")
    public Result readFile(@RequestParam(required = false) List<String> csvPath) {
        try {
            if (csvPath.stream().anyMatch(path -> path.contains("QXSJ"))
                    && csvPath.stream().anyMatch(path -> path.contains("MXFH"))) {
                List<String> result = fileService.readFileMX(csvPath);
                return Result.ok(result);
            }
            if (csvPath.stream().anyMatch(path -> path.contains("QXSJ"))
                    && csvPath.stream().anyMatch(path -> path.contains("TDFH"))
                    && csvPath.stream().anyMatch(path -> path.contains("FBSGFSJ"))) {
                List<String> result = fileService.readFileTD(csvPath);
                return Result.ok(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.error("上传失败,稍后重试:");
    }

    @PostMapping("/mx")
    public Result addLfForecast96lcMX(@RequestParam(required = false) String NETID,
                                      @RequestParam(required = false) String ymd,
                                      @RequestParam(required = false) Integer qxjzh) {
        try {
            fileService.addLfForecast96lcMX(NETID, ymd, qxjzh);
            return Result.ok("成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 测试母线调用模型
     *
     * @param body
     * @return
     */
    @PostMapping("/testAddMX")
    public Result testAddMX(@RequestBody String body) {
        try {
            fileService.testAddLfForecast96lcMX(body);
            return Result.ok("成功");
        } catch (Exception e) {
            return Result.error("解析失败: " + e.getMessage());
        }
    }

    @PostMapping("/testAddMX2")
    public Result testAddMX2(@RequestParam("file") MultipartFile file) {
        try {
            fileService.testAddLfForecast96lcMX2(file);
            return Result.ok("成功");
        } catch (Exception e) {
            return Result.error("转发失败：" + e.getMessage());
        }
    }

    /**
     * 测试统调负荷模型调用
     *
     * @param body
     * @return
     */
    @PostMapping("/testAddTD")
    public Result testAddTD(@RequestBody String body) {
        try {
            fileService.testAddLoadForecastHisTD(body);
            return Result.ok("成功");
        } catch (Exception e) {
            return Result.error("解析失败: " + e.getMessage());
        }
    }

    @PostMapping("/testAddTD2")
    public Result testAddTD2(@RequestParam("file") MultipartFile file) {
        try {
            fileService.testAddLoadForecastHisTD2(file);
            return Result.ok("成功");

        } catch (Exception e) {
            return Result.error("转发失败：" + e.getMessage());
        }
    }

}